package com.wdc.service.impl;


import com.wdc.config.AppConfig;
import com.wdc.constants.Constants;
import com.wdc.entity.dto.SysSettingDto;
import com.wdc.entity.po.UserContact;
import com.wdc.entity.query.SimplePage;
import com.wdc.entity.query.UserContactQuery;
import com.wdc.enums.PageSize;
import com.wdc.entity.vo.PaginationResultVo;
import com.wdc.entity.po.GroupInfo;
import com.wdc.entity.query.GroupInfoQuery;
import com.wdc.enums.ResponseCodeEnum;
import com.wdc.enums.UserContactStatusEnum;
import com.wdc.enums.UserContactTypeEnum;
import com.wdc.exception.BusinessException;
import com.wdc.mapper.GroupInfoMappers;
import com.wdc.mapper.UserContactMappers;
import com.wdc.redis.RedisComponent;
import com.wdc.service.GroupInfoService;
import com.wdc.utils.StringTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: WDC
 * @description:Service
 * @date:2024/09/19
 */
@Service("groupInfoService")
public class GroupInfoServiceImpl implements GroupInfoService {


	@Resource
	private GroupInfoMappers<GroupInfo, GroupInfoQuery> groupInfoMappers;

	@Resource
	private UserContactMappers<UserContact, UserContactQuery> userContactMappers;

	@Resource
	private RedisComponent redisComponent;

	@Resource
	private AppConfig appConfig;

	/**
	 * 根据条件查询列表
	 */
	public List<GroupInfo> findListByParam(GroupInfoQuery query) {
		return this.groupInfoMappers.selectList(query);

	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(GroupInfoQuery query) {
		return this.groupInfoMappers.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVo<GroupInfo> findListByPage(GroupInfoQuery query) {
		Integer count = this.findCountByParam(query);
		Integer pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
		query.setSimplePage(page);
		List<GroupInfo> list = this.findListByParam(query);
		PaginationResultVo<GroupInfo> result = new PaginationResultVo(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	public Integer add(GroupInfo bean) {
		return this.groupInfoMappers.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<GroupInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupInfoMappers.insertBatch(listBean);
	}

	/**
	 * 批量新增或修改
	 */
	public Integer addOrUpdateBatch(List<GroupInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupInfoMappers.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据GroupId查询
	 */
	public GroupInfo getGroupInfoByGroupId(String groupId) {
		return this.groupInfoMappers.selectByGroupId(groupId);
	}

	/**
	 * 根据GroupId更新
	 */
	public Integer updateGroupInfoByGroupId(GroupInfo bean, String groupId) {
		return this.groupInfoMappers.updateByGroupId(bean, groupId);
	}

	/**
	 * 根据GroupId删除
	 */
	public Integer deleteGroupInfoByGroupId(String groupId) {
		return this.groupInfoMappers.deleteByGroupId(groupId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveGroup(GroupInfo groupInfo, MultipartFile avatarFile, MultipartFile avatarCover) throws IOException {
		Date currDate = new Date();
		//新增
		if (StringTools.isEmpty(groupInfo.getGroupId())) {
			GroupInfoQuery groupInfoQuery = new GroupInfoQuery();
			groupInfoQuery.setGroupOwnerId(groupInfo.getGroupOwnerId());
			Integer count = this.groupInfoMappers.selectCount(groupInfoQuery);
			SysSettingDto sysSettingDto = redisComponent.getSysSetting();
			if (count >= sysSettingDto.getMaxGroupCount()) {
				throw new BusinessException("最多支持能创建" + sysSettingDto.getMaxGroupCount() + "个群聊");
			}

			if (null == avatarFile) {
				throw new BusinessException(ResponseCodeEnum.CODE_600);
			}
			groupInfo.setCreateTime(currDate);
			groupInfo.setGroupId(StringTools.getGroupId());
			this.groupInfoMappers.insert(groupInfo);

			//将群组添加为联系人
			UserContact userContact = new UserContact();
			userContact.setStatus(UserContactStatusEnum.FRIEND.getStatus());
			userContact.setContactType(UserContactTypeEnum.GROUP.getType());
			userContact.setContactId(groupInfo.getGroupId());
			userContact.setUserId(groupInfo.getGroupOwnerId());
			userContact.setCreateTime(currDate);
			userContact.setListUpdateTime(currDate);
			userContactMappers.insert(userContact);

		} else {
			GroupInfo dbInfo = this.groupInfoMappers.selectByGroupId(groupInfo.getGroupId());
			if (!dbInfo.getGroupOwnerId().equals(groupInfo.getGroupOwnerId())) {
				throw new BusinessException(ResponseCodeEnum.CODE_600);
			}
			//TODO 修改昵称发送ws消息

		}
		if (null == avatarFile) {
			return;
		}
		String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
		File targetFileFolder = new File(baseFolder + Constants.FILE_FOLDER_AVATAR_NAME);
		if (!targetFileFolder.exists()) {
			targetFileFolder.mkdirs();
		}

		String filepath = targetFileFolder.getPath() + "/" + groupInfo.getGroupId() + Constants.IMAGE_SUFFIX;
		avatarFile.transferTo(new File(filepath));
		avatarCover.transferTo(new File(filepath + Constants.COVER_IMAGE_SUFFIX));


	}

}